﻿using UnityEngine;
using System.Collections;
using utils;
using ui;

public class WorldCreator : MonoBehaviour
{

	// Use this for initialization
	public GameObject basicBody;
	public GameObject wideBody;
	public GameObject smallExplosionParticle;
	public static WorldCreator Instance;

	public static GameObject mainGameContainer;
	public static GameObject endScreen;
	public static GameObject scoreCanvas;

	void initializeWorld ()
	{
		mainGameContainer = GameObject.Find ("GameContainer");
		endScreen = GameObject.Find ("EndScreenCanvas");
		scoreCanvas = GameObject.Find ("ScoreCanvas");

		endScreen.SetActive(false);
		ColorManager.init ();
		ScoreCollision.init();
		RandomPositionManager.init ();
		UiManager.init();
		for (int count = 0; count < maxBlocksOnScreen; count ++) 
		{
			createBlock ();
		}
	}

	void destroyWorld ()
	{
		blocksOnScreen = 0;
		maxBlocksOnScreen = 6;
		totalBlocks = 0;
		UiManager.destroy();
	}

	void Start ()
	{
		Instance = this;
		//change the camera size
		//Camera.main.aspect = GameSettings.GAME_WIDTH/GameSettings.GAME_HEIGHT;

		destroyWorld ();
		initializeWorld ();
	
	}

	private float lastY = 0;
	public static float blocksOnScreen = 0;
	private float maxBlocksOnScreen = 6;
	public static float totalBlocks = 0;
	private GameObject lastBlockOnScreen;

	void addBlocksOnScreen ()
	{

	}

	void Update ()
	{
		if (blocksOnScreen < maxBlocksOnScreen) {
			createBlock ();
		}
	}

	private void createBlock ()
	{
		GameObject block;

		float screenWidth = GameUtils.getScreenWidth ();
		if (totalBlocks % (GameSettings.SMALL_BLOCKS_AMOUNT + 1) == 0) 
		{
			block = BlockCreator.createWideBarrier (wideBody);	
			block.transform.localPosition = new Vector3 (-screenWidth / 2 + RandomPositionManager.getRandomPositionForbigBlock (), 0, 0);
			Transform scoreCollider =  block.transform.GetChild(2);
			scoreCollider.gameObject.name = totalBlocks.ToString();
		} else 
		{
			block = BlockCreator.createSmallBarrier (basicBody);
			block.transform.localPosition = new Vector3 (-screenWidth / 2 + RandomPositionManager.getRandomPositionForSmallBlock (), 0, 0);
		}

		block.transform.parent = mainGameContainer.transform;
		block.transform.localPosition = new Vector3 (block.transform.localPosition.x, lastY + GameSettings.VERTICAL_GAP_BETWEEN_BLOCKS, block.transform.localPosition.z);
		lastY += GameSettings.VERTICAL_GAP_BETWEEN_BLOCKS;
		blocksOnScreen++;
		totalBlocks++;
	}

	public void endLevel()
	{
		//ExplosionBuilder.doExplosion(smallExplosionParticle);
		CounterManager.incremenetCounter(CounterType.DEATH);
		CounterManager.incremenetCounter(CounterType.POINTS,SessionData.score);
		PlayerData.setHighScore(SessionData.score);
		showEndScreen();
		//restartLevel();
	}

	private void showEndScreen()
	{
		mainGameContainer.SetActive(false);
		scoreCanvas.SetActive(false);
		endScreen.SetActive(true);
		endScreen.gameObject.GetComponent<EndScreen>().show();
		//((EndScreen)endScreen.GetComponent(EndScreen)).show();
		//endScreen.SetActive(true);
	}

	public void restartLevel()
	{
		Application.LoadLevel(Application.loadedLevel);
	}
}
